package com.woniu.mealsService.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu.annotation.RequireRole;
import com.woniu.dto.CommentDto;
import com.woniu.form.AddCommentForm;
import com.woniu.form.ReplyCommentForm;
import com.woniu.mealsService.service.CommentService;
import com.woniu.param.AddCommentParam;
import com.woniu.param.replyCommentParam;
import com.woniu.user.client.UserFeignClient;
import com.woniu.user.dto.UserInfoFeignDto;
import com.woniu.utils.JwtUtil;
import com.woniu.utils.PageInfo;
import com.woniu.utils.Result;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * <p>
 * 评价表 前端控制器
 * </p>
 *
 * @author jiang
 * @since 2024年12月09日
 */
@RestController
@RequestMapping("/comment")
@Api(tags = "评价")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    UserFeignClient userFeignClient;
    @ApiOperation( value = "添加评价")
    @PostMapping("/addComment")
    @RequireRole({"学生"})
    public Result<?> addComment(@RequestBody AddCommentForm addCommentForm, @RequestHeader("token") String token) throws Exception{
        Claims claims = JwtUtil.parseToken(token);
        String userId = claims.get("userId").toString();
        String userImg = claims.get("userImg", String.class);
        String userName = claims.get("userName", String.class);
        AddCommentParam bean = BeanUtil.toBean(addCommentForm, AddCommentParam.class);
        bean.setUserImg(userImg);
        bean.setUserName(userName);
        String msg = commentService.addComment(bean,userId);
        return Result.getResult(String.class).setCode(200).setMsg(msg);
    }
    @ApiOperation( value = "删除评价")
    @DeleteMapping("/deleteComment")
    @RequireRole({"商家","学生"})
    public Result<?> deleteComment(@ApiParam(value = "评价id") @RequestParam("orderId") String orderId,@RequestHeader("token") String token) throws Exception{
        Claims claims = JwtUtil.parseToken(token);
        String userId = claims.get("userId").toString();
        boolean b = commentService.deleteComment(orderId,userId);
        return Result.getResult(Boolean.class).setCode(200).setMsg(b?"删除成功":"删除失败");
    }
    @ApiOperation(value = "回复评论")
    @PostMapping("/replyComment")
    @RequireRole("商家")
    public Result<?> replyComment(@RequestBody @Valid ReplyCommentForm replyCommentForm, @RequestHeader("token") String token) throws Exception{
        Claims claims = JwtUtil.parseToken(token);
        String userId = claims.get("userId").toString();
        String msg = commentService.replyComment(BeanUtil.toBean(replyCommentForm, replyCommentParam.class),userId);
        return Result.getResult(String.class).setCode(200).setMsg(msg);
    }
    @ApiOperation(value = "商家获取店铺评论")
    @GetMapping("/getComment")
    @RequireRole("商家")
    public Result<?> getUserComment(@ApiParam("token") @RequestHeader("token") String token) throws Exception{
        Claims claims = JwtUtil.parseToken(token);
        String userId = claims.get("userId").toString();
        List<CommentDto> comment = commentService.getComment(userId);
        return Result.getListResult(CommentDto.class).setCode(200).setMsg("查询用户评论成功").setData(comment);
    }
    @ApiOperation(value = "商家获取店铺全部评论")
    @GetMapping("/getPageStoreComment")
    @RequireRole("商家")
    public Result<?> getPageStoreComment( @ApiParam("页码") Integer pageNum,@ApiParam("每页条数")  Integer pageSize,@ApiParam("token") @RequestHeader String token) throws Exception{
        Claims claims = JwtUtil.parseToken(token);
        String userId = claims.get("userId").toString();
        PageInfo<CommentDto> comment =commentService.getPageComment( userId,pageNum,pageSize);
        return Result.getResult(PageInfo.class).setCode(200).setMsg("查询用户评论成功").setData(comment);
    }
    @ApiOperation(value ="商家获取可回复评论")
    @GetMapping("/getReplyComment")
    @RequireRole("商家")
    public Result<?> getReplyComment(@ApiParam("token") @RequestHeader("token") String token, @ApiParam("页码")  @Min(value = 1,message = "页码不能小于1") Integer pageNum, @Min(value = 1,message = "页码不能小于1")@ApiParam("每页条数")  Integer pageSize) throws Exception{
        Claims claims = JwtUtil.parseToken(token);
        String userId = claims.get("userId").toString();
        PageInfo<CommentDto> comment =commentService.getReplyComment( userId,pageNum,pageSize);
        return Result.getResult(PageInfo.class).setCode(200).setMsg("查询用户评论成功").setData(comment);
    }

    @ApiOperation(value ="用户获取全部评论" )
    @GetMapping("/getCommentDetail")
    @RequireRole({"学生"})
    public Result<?> getCommentDetail(@ApiParam("token") @RequestHeader("token") String token) throws Exception{
        Claims claims = JwtUtil.parseToken(token);
        String userId = claims.get("userId").toString();
        List<CommentDto> comment = commentService.getComment(userId);
        return Result.getListResult(CommentDto.class).setCode(200).setMsg("查询用户评论成功").setData(comment);
    }


    @ApiOperation(value = "店铺id获取店铺评论")
    @GetMapping("/getCommentByUserId")
    public Result<?> getUserIdComment(@ApiParam("storeId") String storeId) throws Exception{
        List<CommentDto> comment = commentService.getCommentByStoreId(storeId);
        return Result.getListResult(CommentDto.class).setCode(200).setMsg("查询店铺评论成功").setData(comment);
    }



}

